package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.TopicBatchDeleteBo;
import com.cskaoyan.bean.bo.TopicCreateBo;
import com.cskaoyan.bean.bo.TopicDeleteBo;
import com.cskaoyan.bean.bo.TopicUpdateBo;
import com.cskaoyan.bean.param.BaseParam;

import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.TopicServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/4 23:01
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicServiceImpl topicService;

    /**
     * /admin/topic/list?page=1&limit=20&sort=add_time&order=desc
     * 此处代码已经使用 TVO 和 PageInfo配合返回;         之前的需求均未使用! todo:即将修改前面的需求;
     * <p>
     * <p>
     * 2022年06月05日21:39:49  chy : todo:修订 list 方法错误;
     * 2022年06月05日22:17:47  chy  : 已修复,  list 方法返回 [] , 而不是返回"[]"
     *
     * @param baseParam
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo topic(BaseParam baseParam, String title, String subtitle) {

        System.out.println("baseParam = " + baseParam);
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        List<TopicVo> topicVoList = topicService.listAll(baseParam, title, subtitle);
        PageInfo pageInfo = new PageInfo(topicVoList);

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        /**
         * 分页插件使用; 与 TVO 使用;
         */
        TVO data = TVO.data(total, pages, baseParam.getLimit(), baseParam.getPage(), topicVoList);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody TopicCreateBo topicCreateBo) {
        System.out.println("topicCreateBo = " + topicCreateBo);
        TopicCreateVo topicCreateVo = topicService.create(topicCreateBo);
        return BaseRespVo.ok(topicCreateVo);
    }

    /**
     * /admin/topic/delete
     *
     * @param topicDeleteBo
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody TopicDeleteBo topicDeleteBo) {

        topicService.deleteOne(topicDeleteBo);
        // 无需返回客户端信心
        return BaseRespVo.ok(null);
    }

    /**
     * /admin/topic/update
     * 接收客户端参数;
     * @param topicUpdateBo
     * @return
     */
    /**
     * 因为复用 VXCollectAddOrDeleteBo 导致了一个 bug; 即: 解析数据异常;  原因是 解析数据不合法 ; 将一个 List 解析为 String ;
     * JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
     * 所以 若要复用 VXCollectAddOrDeleteBo,就得好好看看类型; 不要将就;
     *
     * @param topicUpdateBo
     * @return 2022年06月05日20:56:33 chy
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody TopicUpdateBo topicUpdateBo) {
        System.out.println("topicUpdateBo = " + topicUpdateBo);
        // 复用了 VXCollectAddOrDeleteBo,作为 Vo 和 Po;
        // TopicUpdateBo topicUpdateBo1 =  topicService.updateOneTopic(topicUpdateBo);
        topicService.updateOneTopic(topicUpdateBo);
        return BaseRespVo.ok(topicUpdateBo);
    }

    /**
     * GET  /admin/topic/read?id=363  ;
     *
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        System.out.println("id = " + id);
        /**
         * read 方法必须返回 topicReadVo ; goods 必须是 list;
         */
        TopicReadVo topicReadVo = topicService.read(id);
        return BaseRespVo.ok(topicReadVo);
    }


    @RequestMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody TopicBatchDeleteBo topicBatchDeleteBo) {
        topicService.batchDelete(topicBatchDeleteBo);
        return BaseRespVo.ok(null);
    }

}